//
//  HMAudioPlayerListViewController.m
//  HowToDo
//
//  Created by mynSoo on 2017/1/6.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMAudioPlayerListViewController.h"
#import <UIKit/UIKit.h>
#import "HMListenAnyTimeResModel.h"
#import "HMAudioPlayerListTableViewCell.h"

#define k_AudioPlayerLiskTableViewCell_ID @"HMAudioPlayerListTableViewCell"

@interface HMAudioPlayerListViewController ()
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, weak) UIView *headerView;

@end

@implementation HMAudioPlayerListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self installUI];
    [self installHeaderView];
    [self installTableView];
    [self installTableViewCellRowHeight];
    [self installTableFooterView];
    
}
#pragma mark - 搭建UI
- (void)installTableView
{
    UITableView *tableView = [[UITableView alloc]init];
    tableView.dataSource = self;
    tableView.delegate = self;
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName: k_AudioPlayerLiskTableViewCell_ID bundle:nil] forCellReuseIdentifier:k_AudioPlayerLiskTableViewCell_ID];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.bottom.left.right.offset(0);
    }];
}
- (void)installUI
{
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, 500);
}

- (void)installTableViewCellRowHeight
{
   HMAudioPlayerListTableViewCell *cell = [[UINib nibWithNibName:k_AudioPlayerLiskTableViewCell_ID bundle:nil] instantiateWithOwner:self options:nil].firstObject;
    
    self.tableView.rowHeight = cell.bounds.size.height;
}

- (void)installTableFooterView
{
     UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 1)];
    self.tableView.tableFooterView = footerView;
}

- (void)installHeaderView
{
    UIView *headerView = [[UIView alloc]init];
    self.headerView = headerView;
    headerView.backgroundColor = [UIColor colorWithHex:0xe4e4e4];
    
    // dismissBtn
    UIButton *dismissBtn = [UIButton buttonWithType:0];
    
    [dismissBtn setImage:[UIImage imageNamed:@"new_audio_text_dismiss"] forState:UIControlStateNormal];
    
    [headerView addSubview:dismissBtn];
    
    [dismissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.centerY.offset(0);
    }];
    
    [dismissBtn addTarget:self action:@selector(clickDismissBtn) forControlEvents:UIControlEventTouchDown];
    
    // headerTitleLabel
    UILabel *headerTitleLabel = [[UILabel alloc]init];
    
    headerTitleLabel.text = @"播放列表";
    [headerTitleLabel sizeToFit];
    [headerView addSubview:headerTitleLabel];
    
    [headerTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    // downloadOfAllBtn
    UIButton *downloadOfAllBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [headerView addSubview: downloadOfAllBtn];
    
    [downloadOfAllBtn setTitle:@"下载全部" forState:UIControlStateNormal];
    downloadOfAllBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    
    [downloadOfAllBtn setTitleColor:[UIColor colorWithHex:0xa6a6a6] forState:UIControlStateNormal];
    
    [downloadOfAllBtn sizeToFit];
    
    [downloadOfAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-12);
        make.centerY.offset(0);
    }];
    
    [downloadOfAllBtn addTarget:self action:@selector(clickDownloadOfAllBtn) forControlEvents:UIControlEventTouchDown];
    
    // headerView
    [self.view addSubview:headerView];
    
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(44);
    }];
    
}

- (UIView *)installSectionHeaderView
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)];
    
    headerView.backgroundColor = [UIColor colorWithHex:0xe4e4e4];
    
    // dismissBtn
    UIButton *dismissBtn = [UIButton buttonWithType:0];
    
    [dismissBtn setImage:[UIImage imageNamed:@"new_audio_text_dismiss"] forState:UIControlStateNormal];
    
    [headerView addSubview:dismissBtn];
    
    [dismissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.centerY.offset(0);
    }];
    
    [dismissBtn addTarget:self action:@selector(clickDismissBtn) forControlEvents:UIControlEventTouchDown];
    
    // headerTitleLabel
    UILabel *headerTitleLabel = [[UILabel alloc]init];
    
    headerTitleLabel.text = @"播放列表";
    [headerTitleLabel sizeToFit];
    [headerView addSubview:headerTitleLabel];
    
    [headerTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    // downloadOfAllBtn
    UIButton *downloadOfAllBtn = [UIButton buttonWithType:0];
    
    [headerView addSubview: downloadOfAllBtn];
    
    [downloadOfAllBtn setTitle:@"下载全部" forState:UIControlStateNormal];
    downloadOfAllBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    downloadOfAllBtn.titleLabel.textColor = [UIColor colorWithHex:0xa1a1a1];
    
    [downloadOfAllBtn sizeToFit];
    
    [downloadOfAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-12);
        make.centerY.offset(0);
    }];
    
    [downloadOfAllBtn addTarget:self action:@selector(clickDownloadOfAllBtn) forControlEvents:UIControlEventTouchDown];
    
    // headerView
    return headerView;
    
}

#pragma mark - delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // k_AudioPlayerLiskTableViewCell_ID
    HMAudioPlayerListTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:k_AudioPlayerLiskTableViewCell_ID forIndexPath:indexPath];
    cell.model = self.modelArray[indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.finishSelectBlock)
    {
        self.finishSelectBlock(indexPath);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 触发监听
- (void)clickDismissBtn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)clickDownloadOfAllBtn
{
    
}

@end
